package com.sr.recitewords.service.sysLanguage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sr.recitewords.common.enums.TableNamePrefixEnum;
import com.sr.recitewords.datasource.entities.SysLanguage;
import com.sr.recitewords.datasource.mappers.SysLanguageMapper;
import com.sr.recitewords.datasource.req.SysLanguagePageReq;
import com.sr.recitewords.utils.IdentifierGeneratorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysLanguageServiceImpl extends ServiceImpl<SysLanguageMapper, SysLanguage> implements SysLanguageService {
    @Override
    public Page<SysLanguage> getSysLanguagePage(SysLanguagePageReq req) {
        LambdaQueryWrapper<SysLanguage> lambda = new QueryWrapper<SysLanguage>().lambda();
        lambda.like(StringUtils.isNotEmpty(req.getLanguageName()), SysLanguage::getLanguageName, req.getLanguageName());
        lambda.between(ObjectUtils.isNotEmpty(req.getCreateDateStart()) && ObjectUtils.isNotEmpty(req.getCreateDateEnd()), SysLanguage::getCreateDate,
                req.getCreateDateStart(), req.getCreateDateEnd());
        return page(new Page<>(req.getPage(), req.getSize()), lambda);
    }

    @Override
    public boolean addSysLanguage(SysLanguage sysLanguage) {
        sysLanguage.setLanguageId(IdentifierGeneratorUtil.getOwnId(TableNamePrefixEnum.SYS_LANGUAGE));
        sysLanguage.setCreateDate(new Date());
        return save(sysLanguage);
    }

    @Override
    public boolean addSysLanguageList(List<SysLanguage> sysLanguageList) {
        Date date = new Date();
        for (SysLanguage sysLanguage : sysLanguageList) {
            sysLanguage.setLanguageId(IdentifierGeneratorUtil.getOwnId(TableNamePrefixEnum.SYS_LANGUAGE));
            sysLanguage.setCreateDate(date);
        }
        return saveBatch(sysLanguageList);
    }
}
